﻿namespace Json2Model
{
    public static class Converter
    {
        private static AutoSerialize<Dictionary<string, string>> AutoSerializeType2NameDic = new AutoSerialize<Dictionary<string, string>>("Json2Model_Type2NameDic");
        public static Dictionary<string, string> Type2NameDic => AutoSerializeType2NameDic.Value;
        public static Type ToType(JTokenType jTokenType)
        {
            switch (jTokenType)
            {
                case JTokenType.String:
                    return typeof(string);
                case JTokenType.Boolean:
                    return typeof(bool);
                case JTokenType.Guid:
                    return typeof(Guid);
                case JTokenType.Integer:
                    return typeof(int);
                case JTokenType.Float:
                    return typeof(float);
                case JTokenType.Date:
                    return typeof(DateTime);
                case JTokenType.TimeSpan:
                    return typeof(TimeSpan);
                case JTokenType.Uri:
                    return typeof(Uri);
                case JTokenType.Null:
                    return typeof(string);
                default:
                    return typeof(string);
            }
        }
        public static string ToTypeName(Type type)
        {
            if (Type2NameDic.Count == 0)
            {
                var dic = new Dictionary<string, string>
                {
                    {typeof(string).FullName!,"string"},
                    {typeof(int).FullName!,"int"},
                    {typeof(float).FullName!,"float"},
                    {typeof(bool).FullName!,"bool"},
                    {typeof(Guid).FullName!, "Guid"},
                    {typeof(DateTime).FullName!, "DateTime"},
                    {typeof(TimeSpan).FullName!, "TimeSpan"},
                    {typeof(Uri).FullName!, "Uri"}
                };
                AutoSerializeType2NameDic.Value = dic;
            }
            if (Type2NameDic.ContainsKey(type.FullName!))
                return Type2NameDic[type.FullName!];
            return "string";
        }

        public static string FieldPrefix { get; set; } = string.Empty;
        public static string PropertyNameFormat(string name)
        {
            if (!string.IsNullOrWhiteSpace(FieldPrefix))
                return name;
            if (string.IsNullOrEmpty(name))
                return string.Empty;
            name = name.ToLower();
            if (name.Contains("_"))
            {
                string temp = string.Empty;
                foreach (string str in name.Split('_'))
                {
                    temp += FirstToUpper(str);
                }
                return temp;
            }
            return FirstToUpper(name);
        }
        public static string FieldNameFormat(string name)
        {
            name = PropertyNameFormat(name);
            name = name.Substring(0, 1).ToLower() + name.Substring(1);
            if (!string.IsNullOrWhiteSpace(FieldPrefix))
                return FieldPrefix + name;
            return name;
        }
        public static string TypeNameFormat(string name)
        {
            return FirstToUpper(name);
        }

        public static string ArrayItemNameFormat(string listName)
        {
            if (listName.ToLower().EndsWith("list"))
                return TypeNameFormat(listName.Substring(0, listName.Length - 4));
            if (listName.ToLower().EndsWith('s'))
                return TypeNameFormat(listName.Substring(0, listName.Length - 1));
            else
                return TypeNameFormat(listName + "Item");
        }
        /// <summary>
        /// 首字母大写
        /// </summary>
        public static string FirstToUpper(string str)
        {
            return str.Substring(0, 1).ToUpper() + str.Substring(1);
        }
    }
}
